#ifndef __GWSINGLETONHELPER_H__
#define __GWSINGLETONHELPER_H__

#include "gwcommon.h"

/**
*@file       gwsingletonhelper.hpp
*@brief      singleton implement
*
*@author     graywind
*@version    1.0.0.0
*@data       2012/01/06
*/

///@brief   thread local storage implement
#define GW_DeclareSingleton(type) \
private:\
        type();\
        type(const type& rhs){};\
        ~type();\
public: \
        ThreadLocalStorage static type * g_pSingleton; \
        static type * Instance() { gwAssert(g_pSingleton); return g_pSingleton; }; \
        static void Create() { gwAssert(gwNULL == g_pSingleton); gw_new(type);};\
        static void Destroy() {gwAssert(g_pSingleton); gw_delete(g_pSingleton); };\
        static bool HasInstance() { return 0 != g_pSingleton; }; \
private:

///@brief    normal singleton implement
#define GW_DeclareInterfaceSingleton(type) \
private:\
        type();\
        type(const type& rhs){};\
        ~type();\
public: \
        static type * g_pSingleton; \
        static type * Instance() { gwAssert(g_pSingleton); return g_pSingleton; }; \
        static void Create() { gwAssert(gwNULL == g_pSingleton); gw_new(type);};\
        static void Destroy() {gwAssert(g_pSingleton); gw_delete(g_pSingleton); };\
        static bool HasInstance() { return 0 != g_pSingleton; }; \
private:

#define GW_ImplementSingleton(type) \
        ThreadLocalStorage type * type::g_pSingleton = 0;

#define GW_ImplementInterfaceSingleton(type) \
        type * type::g_pSingleton = 0;

#define GW_ConstructSingleton \
        gwAssert(0 == g_pSingleton); g_pSingleton = this;

#define GW_ConstructInterfaceSingleton \
        gwAssert(0 == g_pSingleton); g_pSingleton = this;

#define GW_DestructSingleton \
        gwAssert(g_pSingleton); g_pSingleton = 0;

#define GW_DestructInterfaceSingleton \
        gwAssert(g_pSingleton); g_pSingleton = 0;

#endif
